
// 所有请求接口

/**
  code	message
  200	成功
  205	操作失败
  401	发生异常
  402	系统错误
  403	参数错误
 */
const baseUrl = 'https://www.youdingb.com/';

/**
 * post request
 * url: 请求地址string
 * params：参数Object
 */
function postReq(url, params){
  return new Promise((resolve, reject) => {
    let timer = null;
    let data = "";
    wx.request({
      url: `${baseUrl}${url}`,
      method: "POST",
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded" // 默认值
      },
      success: res => {
        // test 待解开注释
        resolve(res);
        // if (res.code == 200) {
        //   data = true;
        //   clearTimeout(timer);
        //   resolve(res.data);
        // } else {
        //   resolve(null);
        // }
      },
      error: error => {
        reject(error);
      }
    });
    timer = setTimeout(() => {
      if (!data) {
        wx.hideLoading();
        // wx.showToast({
        // 	title:'连接超时',
        // 	image:'/pages/assets/images/false.png'
        // })
        console.log("连接超时了");
      }
      clearTimeout(timer);
    }, 10000);
  });
}

/** 
 * get request
 * url: 请求地址string
 * params：参数Object
 */
function getReq(){
  return new Promise((resolve, reject) => {
    let timer = null;
    let data = "";
    wx.request({
      url: `${baseUrl}${url}`,
      method: "POST",
      data: params,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        if (res.code == 200) {
          data = true;
          clearTimeout(timer);
          resolve(res.data);
        } else {
          rejectserver
        }
      },
      error: error => {
        reject(error);
      }
    });
    time = setTimeout(() => {
      if (!data) {
        wx.hideLoading();
        // wx.showToast({
        // 	title:'连接超时',
        // 	image:'/pages/assets/images/false.png'
        // })
        console.log("连接超时了");
      }
      clearTimeout(timer);
    }, 10000);
  });
}



export function sendUserInfo(params){
  return postReq('shouquan/index.php/Home/Index/getuserinfo',params);
}


// 1.
export function test1(params){
  return getReq('url',params);
}

// 2. 
export function test2(params){
  return new Promise((resolve,reject)=>{
    postReq('url', params).then(
      res => {
        if(res){
          resolve(res);
        }
      },
      error => {
        reject(rrror);
      })
  })
}
